﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BO;
using EntityFramework;
using ProjectQuaDep.Ajax;
using System.Data;

namespace ProjectQuaDep.Control
{
    public partial class HomeProductFocus : System.Web.UI.UserControl
    {
        public int ZoneFocus { get; set; }

        private const string ProductFocusItemFormat = @"<div class=""item {0}"">
                    {1}
                    <div class=""name"">
                        {2}</div>
                    <div class=""img"">
                        <a href=""{4}"" title=""{3}"">
                            <img title=""{10}"" alt=""{3}"" src=""{5}"" /></a>
                    </div>
                    <div class=""code-name"">
                        {6}</div>
                    <div class=""desc"">
                        {7}</div>
                    <div class=""shopping"">
                        <div class=""price"">
                            <div class=""current"">
                                {8}</div>
                            <div class=""discount"">
                                {9}</div>
                        </div>
                        <a href=""javascript:void(0);"" class=""cart""></a>
                        <div class=""clear""></div>
                    </div>
                    <div class=""chitietshow"">
                        <a href=""{4}"" title=""{3}"">Chi tiết »</a>
                    </div>
                </div>";
        private const string ProductNewFormat = "<i class=\"new\"></i>";

        protected void Page_Load(object sender, EventArgs e)
        {
            DataTable dt = BO.ProductUserBO.ProductUBO.GetTopHomeProductBO("3", ZoneFocus.ToString());
            if (dt.Rows.Count > 0)
            {
                DataTable dtZone = BO.ProductUserBO.ProductUBO.GetInfomationZoneBO(ZoneFocus.ToString());
                if (dtZone.Rows.Count > 0)
                {
                    ltrFocus.Text = dtZone.Rows[0][Const.tbZoneProduct.title].ToString();//ZoneProductBO.GetZoneProductNameById(ZoneFocus);
                }
                var sb = new StringBuilder();
                var count = 1;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string cls = (i + 1) % 3 == 0 ? "mr0" : string.Empty;
                    long date = 0;
                    long.TryParse(dt.Rows[i][Const.tbProducts.EndDateNew].ToString(), out date);
                    var pNew = date > 0 && date >= long.Parse(DateTime.Now.ToString("yyyyMMdd")) ? ProductNewFormat : string.Empty;

                    var price = int.Parse(dt.Rows[i][Const.tbProducts.TypeSale].ToString()) == 1 ? "Giá: Liên hệ" : Utils.ConvertMoneyNice(dt.Rows[i][Const.tbProducts.Price].ToString(), BO.Const.FormatMoney);
                    var sale = int.Parse(dt.Rows[i][Const.tbProducts.TypeSale].ToString()) == 1 ? string.Empty : Utils.ConvertMoneyNice(dt.Rows[i][Const.tbProducts.Sale].ToString(), BO.Const.FormatMoney);
                    var sapo = dt.Rows[i][Const.tbProducts.Sapo].ToString();
                    if (int.Parse(dt.Rows[i][Const.tbProducts.Sale].ToString()) == 0)
                    {
                        sale = "";
                    }
                    string sTitle = dt.Rows[i][Const.tbProducts.Title].ToString();
                    sb.AppendFormat(ProductFocusItemFormat, cls
                        , pNew
                        , sTitle
                        , HttpUtility.HtmlEncode(sTitle)
                        , dt.Rows[i][Const.tbProducts.Url].ToString()
                        , BO.Utils.CropImage(172, 140, BO.Utils.GetOneImagesByArr(dt.Rows[i][Const.tbProducts.Avatar].ToString()))
                        , dt.Rows[i][Const.tbProducts.ProductCode].ToString()
                        , BO.Utils.getSubStringNice(dt.Rows[i][Const.tbProducts.Sapo].ToString(), 50)
                        , price
                        , sale
                        , sapo);
                    count++;
                }
                ltrListFocus.Text = sb.ToString();
                phProductFocus.Visible = true;
            }
            //var lst = ProductsBO.GetTopProductIsFocus(3, ZoneFocus);
            //if (lst.Count != 0)
            //{
            //    ltrFocus.Text = ZoneProductBO.GetZoneProductNameById(ZoneFocus);
            //    var sb = new StringBuilder();
            //    var count = 1;
            //    foreach (var item in lst)
            //    {
            //        var cls = count % 3 == 0 ? "mr0" : string.Empty;
            //        long date = 0;
            //        long.TryParse(item.EndDateNew, out date);
            //        var pNew = date > 0 && date >= long.Parse(DateTime.Now.ToString("yyyyMMdd")) ? ProductNewFormat : string.Empty;

            //        var price = item.TypeSale == 1 ? "Liên hệ" : Utils.ConvertMoneyNice(item.price.ToString(), BO.Const.FormatMoney);
            //        var sale = item.TypeSale == 1 ? string.Empty : Utils.ConvertMoneyNice(item.sale.ToString(), BO.Const.FormatMoney);
            //        if (int.Parse(item.sale.ToString()) == 0)
            //        {
            //            sale = "";
            //        }
            //        sb.AppendFormat(ProductFocusItemFormat, cls
            //            , pNew
            //            , item.Title
            //            , HttpUtility.HtmlEncode(item.Title)
            //            , item.Url
            //            , BO.Utils.CropImage(172, 172, BO.Utils.GetOneImagesByArr(item.Avatar))
            //            , item.ProductCode
            //            , BO.Utils.getSubStringNice(item.Sapo,50)
            //            , price
            //            , sale);
            //        count++;
            //    }
            //    ltrListFocus.Text = sb.ToString();
            //    phProductFocus.Visible = true;
            //}
        }
    }
}